package com.mt.course.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.common.constants.BaseErrorConstants;
import com.mt.common.util.ConvertUtil;
import com.mt.course.api.service.ICourseCategoryService;
import com.mt.course.entity.CourseCategoryEntity;
import com.mt.course.entity.CourseEntity;
import com.mt.course.service.model.CourseCategoryServiceModel;
import com.mt.course.service.model.CourseServiceModel;
import org.springframework.stereotype.Service;

@Service
public class CourseCategoryServiceImpl implements ICourseCategoryService {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CourseCategoryServiceImpl.class);

    @Autowired
    private CourseCategoryServiceModel courseCategoryServiceModel;

    @Autowired
    private CourseServiceModel courseServiceModel;

    public ServiceResult<CourseCategoryEntity> findInfo(Map<String, Object> params) {
        // 查询用户信息
        ServiceResult<CourseCategoryEntity> result = null;
        // 该参数标识前段是否需要权限数据
        try {
            result = courseCategoryServiceModel.selectInfo(params);
        } catch (Exception e) {
            LOGGER.error("[CourseCategoryServiceImpl][findInfo]：query findInfo occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询详情错误");
        }
        return result;
    }

    public ServiceResult<List<CourseCategoryEntity>> findList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        ServiceResult<List<CourseCategoryEntity>> result = new ServiceResult<List<CourseCategoryEntity>>();

        boolean withTopCategoryCourse = ConvertUtil.toBool(params.get("withTopCategoryCourse"), false);
        boolean withCategoryCourse = ConvertUtil.toBool(params.get("withCategoryCourse"), false);

        Integer isRecommend = ConvertUtil.toInt(params.get("isRecommend"), 0);
        List<CourseCategoryEntity> courseCategoryList = null;
        try {
            result = courseCategoryServiceModel.selectList(params, pagerInfo);
            courseCategoryList = result.getResult();
            if ((withTopCategoryCourse || withCategoryCourse) && courseCategoryList != null
                && courseCategoryList.size() > 0) {
                for (CourseCategoryEntity entity : courseCategoryList) {
                    Map<String, Object> courseParams = new HashMap<String, Object>();
                    if (withTopCategoryCourse && entity.getParentId() == 0) {
                        courseParams.put("topCategoryId", entity.getCourseCategoryId());
                    }
                    if (withCategoryCourse) {
                        courseParams.put("categoryId", entity.getCourseCategoryId());
                    }
                    if (isRecommend > 0) {
                        courseParams.put("isRecommend", isRecommend);
                    }
                    ServiceResult<List<CourseEntity>> courseResult = courseServiceModel.selectList(courseParams, null);
                    List<CourseEntity> courseList = courseResult.getResult();
                    entity.setCourseList(courseList);
                }
                result.setResult(courseCategoryList);
            }
        } catch (Exception e) {
            LOGGER.error("[CourseCategoryServiceImpl][findList]：query findList occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询列表错误");
        }

        return result;
    }

    public ServiceResult<Integer> doInsert(CourseCategoryEntity courseCategoryEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = courseCategoryServiceModel.insert(courseCategoryEntity);
        } catch (Exception e) {
            LOGGER.error("[CourseCategoryServiceImpl][doInsert]：query doInsert occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "创建信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdateRelate(CourseCategoryEntity courseCategoryEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = courseCategoryServiceModel.updateRelate(courseCategoryEntity);
        } catch (Exception e) {
            LOGGER.error("[CourseCategoryServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "编辑信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doDeleteById(Integer courseCategoryId) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = courseCategoryServiceModel.deleteByPrimaryKey(courseCategoryId);
        } catch (Exception e) {
            LOGGER.error("[CourseCategoryServiceImpl][doDeleteById]：query doDeleteById occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "删除信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdate(CourseCategoryEntity courseCategoryEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = courseCategoryServiceModel.updateByPrimaryKeySelective(courseCategoryEntity);
        } catch (Exception e) {
            LOGGER.error("[CourseCategoryServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "更新信息错误.");
        }
        return result;
    }

}
